    		import org.tuskit.model.User;
    		import mx.events.DropdownEvent;
    		import org.tuskit.control.EventNames;
    		import org.tuskit.util.CairngormUtils;
    		import mx.collections.ListCollectionView;
    		import mx.collections.ArrayCollection;
    		import org.tuskit.model.TuskitModelLocator;
	    	
	    	import mx.rpc.events.ResultEvent;
	    	import mx.events.CloseEvent;
	    	import mx.controls.Alert;
    	    import mx.validators.Validator;
	    	import org.tuskit.model.User;
	    	
	    	[Bindable]
	    	private var _model:TuskitModelLocator = TuskitModelLocator.getInstance();		 	
		 	
	    	[Bindable]
	    	private var user_model:User = new User();
	    	
	    	[Bindable]
	    	private var isAdmin:Boolean = false;
	    	
	    	[Bindable]
	    	private var isDeveloper:Boolean = false;
	    	
	    	[Bindable]
	    	private var isCustomer:Boolean = false;
	    	
	    	private const ANY_GROUP			:int = 0;
	    	private const ADMIN_GROUP		:int = 1;
	    	private const DEVELOPER_GROUP	:int = 2;
	    	private const CUSTOMER_GROUP		:int = 3;
	    	
	    	private const SELECT_GROUP:Array = [
	    		{label: "Any", data: ANY_GROUP, filter: emptyFilter},
	    		{label: "Admin", data: ADMIN_GROUP, filter: adminFilter},
	    		{label: "Developer", data: DEVELOPER_GROUP, filter: developerFilter},
	    		{label: "Customer", data: CUSTOMER_GROUP, filter: customerFilter}
			];
						
	    	private var nextState:Function = stateCreateUser;
			
			private function emptyFilter(user:User):Boolean {
				var name:String = user.first_name.toLowerCase() + " " + user.last_name.toLowerCase();
				return name.indexOf(filterInput.text) != -1;
			}
			
			private function adminFilter(user:User):Boolean {
				var name:String = user.first_name.toLowerCase() + " " + user.last_name.toLowerCase();
				return (name.indexOf(filterInput.text) != -1 && user.isAdmin());				
			}
			
			private function developerFilter(user:User):Boolean {
				var name:String = user.first_name.toLowerCase() + " " + user.last_name.toLowerCase();
				return name.indexOf(filterInput.text) != -1 && user.isDeveloper();				
			}

			private function customerFilter(user:User):Boolean {
				var name:String = user.first_name.toLowerCase() + " " + user.last_name.toLowerCase();
				return name.indexOf(filterInput.text) != -1 && user.isCustomer();				
			}
			
			private function handleFilterInputChange():void {
	    		if (_model.users.filterFunction === null) {
	    			_model.users.filterFunction = emptyFilter;
	    		}
				_model.users.refresh();
			}
			
			private function handleUserFilter(event:DropdownEvent):void {
				var idx:int = event.currentTarget.selectedIndex;
				_model.selectedUserId = -1;
				usersGrid.selectedItem = null;
				_model.users.filterFunction = SELECT_GROUP[idx].filter;
				filterInput.text = "";
				clearUser();
				nextState=stateCreateUser;
				changeState();
				_model.users.refresh();
			}

	    	public function deleteUser(usr:User):void {
	    		clearUser();
	    		nextState=stateCreateUser;
	    		changeState();
	    		CairngormUtils.dispatchEvent(EventNames.DELETE_USER,usr);
	    	} 
            			
			private function init():void {
				_model.getUsersGroupsMemberships();
				changeState();
			}

			private function changeState():void{
				nextState.call();
			}
			
			private function stateCreateUser():void {
				isAdmin = false;
				isDeveloper = false;
				isCustomer = false;
	    		newUserB.visible=false;
	    		userInputP.title="Create New User";
				userInputP.styleName = "newUser";
				loginTI.editable = true;
				loginTI.styleName = "inputEnabled";
	    		focusManager.setFocus(loginTI);
	    		saveB.label="Create";
	    		passwordFI.required=true;
	    		passwordConfirmationFI.required=true;
	    		passwordConfirmationValidator.required=true;
	    		passwordValidator.required=true;
			}
			
			private function stateUpdateUser():void {
				userInputP.styleName = "updateUser";
				loginTI.editable=false;
				loginTI.styleName="inputDisabled";                
				newUserB.visible=true;
				saveB.label="Update";
				focusManager.setFocus(firstNameTI);
				
				user_model = _model.getUser(_model.selectedUserId);
				userInputP.title = "Update User " + user_model.first_name +
					" " + user_model.last_name + " (aka " +
				    user_model.login + ")";
				isAdmin = user_model.isAdmin();
				isDeveloper = user_model.isDeveloper();
				isCustomer = user_model.isCustomer();
	    		passwordFI.required=false;
	    		passwordConfirmationFI.required=false;
	    		passwordConfirmationValidator.required=false;
	    		passwordValidator.required=false;
			}
 	 
	    	private function clearUser():void {
	    		isAdmin = adminCB.selected;
	    		isDeveloper = developerCB.selected;
	    		isCustomer = customerCB.selected;
	    		user_model = new User();
	    		isAdmin = false
	    		isDeveloper = false;
	    		isCustomer = false;
	    	}
	    	
	    	public function editUser():void {
				_model.selectedUserId = usersGrid.selectedItem.id;	    		
	    		nextState = stateUpdateUser;
	    		changeState();
	    	}
	    	
	    	private function prepareGroupsToSave():Object {
	    		var retval:Object = {'toCreate':[],'toDelete':[]};
	    		if (user_model.id == User.UNSAVED_ID){
	    			if (adminCB.selected) {
	    				retval['toCreate'].push(_model.getGroup(TuskitModelLocator.ADMIN));
	    			}
	    			if (developerCB.selected) {
	    				retval['toCreate'].push(_model.getGroup(TuskitModelLocator.DEVELOPER));
	    			}
	    			if (customerCB.selected) {
	    				retval['toCreate'].push(_model.getGroup(TuskitModelLocator.CUSTOMER));
	    			}
	    		} else {
					if (adminCB.selected) {
						if (!user_model.isAdmin()) {
							retval['toCreate'].push(_model.getGroup(TuskitModelLocator.ADMIN));
						}
					} else {
						if (user_model.isAdmin()) {
							retval['toDelete'].push(_model.getGroup(TuskitModelLocator.ADMIN));
						}
					}

					if (developerCB.selected) {
						if (!user_model.isDeveloper()) {
							retval['toCreate'].push(_model.getGroup(TuskitModelLocator.DEVELOPER));
						}
					} else {
						if (user_model.isDeveloper()) {
							retval['toDelete'].push(_model.getGroup(TuskitModelLocator.DEVELOPER));
						}
					}
					
					if (customerCB.selected) {
						if (!user_model.isCustomer()) {
							retval['toCreate'].push(_model.getGroup(TuskitModelLocator.CUSTOMER));
						}
					} else {
						if (user_model.isCustomer()) {
							retval['toDelete'].push(_model.getGroup(TuskitModelLocator.CUSTOMER));
						}
					}
	    		}
				return retval;
	    	}
	    	
	    	
	    	private function saveUser():void {
				var results:Array = Validator.validateAll([
		            usernameValidator,
		            firstNameValidator,
		            lastNameValidator,
		            emailValidator]);
		        if (results.length > 0) {
		            Alert.show("Please correct the validation errors highlighted " +
		                "on the form.", "Account Not Created");
		            return;
		        }
	    		var groupsData:Object = prepareGroupsToSave();
	    		var userToSave:User = user_model;
	    		var userPackage:Object = {'user':userToSave, 'groups':groupsData};
	    		userToSave.login = loginTI.text;
				userToSave.first_name = firstNameTI.text;
				userToSave.last_name = lastNameTI.text;
				userToSave.phone = phoneTI.text;
				userToSave.email = emailTI.text;
				userToSave.password = passwordTI.text;
				userToSave.password_confirmation = confirmPasswordTI.text;
				if (userToSave.id == User.UNSAVED_ID) {					
					CairngormUtils.dispatchEvent(EventNames.CREATE_USER, userPackage);
					nextState = stateUpdateUser;					
				}
				else if (userToSave.id > 0) {
					CairngormUtils.dispatchEvent(EventNames.UPDATE_USER, userPackage);
					nextState = stateUpdateUser;
				}
	    	}
	    	
	    	public function getUserGridSelectedIndex(userId:int):void {
	    		if (userId != -1) {
		    		var selectedUser:User = _model.getUser(userId);
		    		usersGrid.selectedIndex = _model.users.getItemIndex(selectedUser);
		    		user_model = selectedUser;
		    		stateUpdateUser();
	    		} 
	    		else {
	    			user_model= new User();
	    			stateCreateUser();	    			
	    		}
	    	}
	    	
	    	private function getUsersGridSelectedIndex(isInTransitionState:Boolean):int {
	    		if (!isInTransitionState && _model.selectedUserId != -1) {
	    			var selectedUser:User = _model.getUser(_model.selectedUserId);
	    			changeState();
	    			return _model.users.getItemIndex(selectedUser);
	    		} else return -1;
	    	}
